package org.whb.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.whb.basic.service.impl.BaseServiceImpl;
import org.whb.org.domain.Department;
import org.whb.org.mapper.DepartmentMapper;
import org.whb.org.service.IDepartmentService;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    public void update(Department department) {
        String dirPath="";
        if (department.getParent()==null){ //说明添加的是顶级部门
            dirPath="/"+department.getId();
        }else { //说明添加的不是顶级部门
            Department parent = department.getParent();
            String parentDirPath = departmentMapper.loadById(parent.getId()).getDirPath();
            dirPath=parentDirPath+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryDeptTree() {
        return departmentMapper.loadDeptTree();
    }
}
